import Entity.Car;
import Factory.ICarFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;

public class Main {
//    public static void main(String[] args) {
//        try {
//            ICarFactory factory;
//
//            //方法一：字符串读取
//            //Class classReflection = Class.forName("Factory.FactoryBenc");
//
//            //方法二：运用反射和xml进行工厂读取
//            Class classReflection = Class.forName(ReadClassNameFromXML());
//
//            Object objcetReflection = classReflection.newInstance();
//
//
//            factory = (ICarFactory) objcetReflection;
//            Car car = factory.CarFactoryMethod();
//            System.out.printf(car.showCarInfo());
//
//        }catch (Exception e){
//            System.out.println(e.getMessage());
//        }
//    }

    //    public static String ReadClassNameFromXML() {
//        String className = "";
//        try {
//            //读取XML文件
//            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
//            DocumentBuilder builder = dFactory.newDocumentBuilder();
//            Document doc;
//            doc = builder.parse(new File("class_4//src//config.xml"));
//
//            //获取包含类名的文本结点
//            NodeList nl = doc.getElementsByTagName("className");
//            Node classNode = nl.item(0).getFirstChild();
//            className = classNode.getNodeValue();
//
//
//        } catch (Exception e) {
//            e.printStackTrace();
//
//        } finally {
//
//        }
//        return className;
//    }

    //这里是改进的依靠循环编号的方式实现的自动读取
    public static void main(String[] args) {
        try {
            for (int i = 1; i <= ReadClassCountFromXML(); i++) {
                ICarFactory factory;

                // 读取XML中的类名
                String className = ReadClassNameFromXML("car" + i);
                if (!className.isEmpty()) {
                    Class classReflection = Class.forName(className);
                    Object objectReflection = classReflection.newInstance();

                    factory = (ICarFactory) objectReflection;
                    Car car = factory.CarFactoryMethod();
                    System.out.println(car.showCarInfo());
                } else {
                    System.out.println("未找到id编号为car" + i + "的汽车");
                }
            }

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public static String ReadClassNameFromXML(String id) {
        String className = "";
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("class_4//src//config.xml"));

            // 获取指定ID的类名节点
            NodeList nl = doc.getElementsByTagName("className");
            for (int i = 0; i < nl.getLength(); i++) {
                Element element = (Element) nl.item(i);
                String carId = element.getAttribute("id");
                if (carId.equals(id)) {
                    className = element.getTextContent();
                    break;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return className;
    }

    //返回xml里有多少辆车
    public static int ReadClassCountFromXML() {
        int classCount=0;
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("class_4//src//config.xml"));

            // 获取指定ID的类名节点
            NodeList nl = doc.getElementsByTagName("className");
            classCount=nl.getLength();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return classCount;
    }
}




